package com.cz.ssyx.activity.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cz.ssyx.activity.service.ActivityInfoService;
import com.cz.ssyx.common.result.Result;
import com.cz.ssyx.model.activity.ActivityInfo;
import com.cz.ssyx.model.activity.ActivityRule;
import com.cz.ssyx.model.product.SkuImage;
import com.cz.ssyx.model.product.SkuInfo;
import com.cz.ssyx.vo.activity.ActivityRuleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 活动表 前端控制器
 * </p>
 *
 * @author Joker
 * @since 2024-04-20
 */
@Api(tags = "营销活动管理")
//@CrossOrigin
@RestController
@RequestMapping("/admin/activity/activityInfo")
public class ActivityInfoController {

    @Autowired
    private ActivityInfoService activityInfoService;


    @ApiOperation("活动分页列表")
    @GetMapping("/{page}/{limit}")
    public Result getPageList(@PathVariable Long page, @PathVariable Long limit) {
        Page<ActivityInfo> pageParam = new Page<>(page, limit);
        IPage<ActivityInfo> pageModel = activityInfoService.selectPage(pageParam);
        return Result.ok(pageModel);
    }

    @ApiOperation("获取活动规则")
    @GetMapping("/findActivityRuleList/{id}")
    public Result findActivityRuleList(@PathVariable Long id) {
        Map<String, Object> activityRuleListMap = activityInfoService.findActivityRuleList(id);
        return Result.ok(activityRuleListMap);
    }

    @ApiOperation("根据关键词查询商品信息")
    @GetMapping("/findSkuInfoByKeyword/{keyword}")
    public Result findSkuInfoByKeyword(@PathVariable String keyword) {
       List<SkuInfo> skuInfoList = activityInfoService.findSkuInfoByKeyword(keyword);
       return Result.ok(skuInfoList);
    }

    @ApiOperation("保存活动规则")
    @PostMapping("/saveActivityRule")
    public Result saveActivityRule(@RequestBody ActivityRuleVo activityRuleVo) {
        activityInfoService.saveActivityRule(activityRuleVo);
        return Result.ok(null);
    }

    @ApiOperation("新增活动")
    @PostMapping("/save")
    public Result save(@RequestBody ActivityInfo activityInfo) {
        activityInfoService.save(activityInfo);
        return Result.ok(null);
    }

    @ApiOperation("id得到活动信息")
    @GetMapping("/get/{id}")
    public Result getById(@PathVariable Long id) {
        ActivityInfo activityInfo = activityInfoService.getById(id);
        activityInfo.setActivityTypeString(activityInfo.getActivityType().getComment());
        return Result.ok(activityInfo);
    }

    @ApiOperation("修改活动信息")
    @PutMapping("/update")
    public Result update(@RequestBody ActivityInfo activityInfo) {
        activityInfoService.updateById(activityInfo);
        return Result.ok(null);
    }

    @ApiOperation("删除活动")
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable Long id) {
        activityInfoService.removeById(id);
        return Result.ok(null);
    }

    @ApiOperation("批量删除活动")
    @DeleteMapping("/batchRemove]{idList}")
    public Result batchRemove(@PathVariable List<Long> idList) {
        activityInfoService.removeByIds(idList);
        return Result.ok(null);
    }

}

